<? 
	$title="User View All"; 
	$pageid="1";
	$accessid="a00";
	require("required/dbconnect.php");	
	require("required/function.php");
	require("required/header.php");
	checkAccess("allow_user_management",1);
	
?>
<script src="required/js/jquery-1.4.2.js"></script>
<script src="required/js/common_javascript.js"></script>
<?	
	if(isset($_POST['btnsubmit']))
	{
		$branch_id=$_POST['branch_id'];
		$user_id=$_POST['user_id'];
		$password=$_POST['password'];
		$role_id=$_POST['user_role'];
				
		$chk=mysql_query("select * from os_user where user_id='$user_id'")or die(mysql_error());
			
		if(mysql_num_rows($chk)>0)
		{
			$msg="<div class='dw_failed'>$user_id already exist! </div>"; 
		}
		else
		{			
			$insert=mysql_query("insert into os_user(user_id,user_password,user_level,role_id,branch_id)
								 values('$user_id','$password',2,$role_id,$branch_id)")or die(mysql_error());
				
			$msg="<div class='dw_success'> User added successfully! </div>"; 
		}
		echo "<script>displayMsg('divMsg')</script>";	
	}
	else
	{
		$msg="";
	}
	
	
	
	
	$get_user=mysql_query("select * from os_user where user_id<>'admin' order by user_id asc")or die(mysql_error());
	
	
?>
<table width="100%">

<td align="left" valign="top">
	<font class="title">View All User</font>
	<br />
    <div id="divMsg"><?= $msg ?></div>
    <br /> 
	<table width="95%" cellpadding="2" cellspacing="1" class="solidborder" >
		<? if(mysql_num_rows($get_user)==0){ ?>
		<tr>
		 <td height="200" valign="middle" colspan="8" align="center">
			<font color="#FF0000">- No user found -</font><br>
			
		 </td>
		</tr>
		<? }else{ ?>
		
		
		<tr height="20" align="center" class="table_header" >
			<Td width="2%">No</Td><td width="25%">Username</td>
		 
		  <td width="18%">Role</td>
          <td width="19%">Status</td>
		  <td width="16%">Action</td>
		</tr>
		<?
			$no=1;
			while($a=mysql_fetch_array($get_user))
			{
				if($no%2==0)
				{
					$class1="table_item_even";
					$class2="table_item_odd";
				}
				else
				{
					$class1="table_item_odd";
					$class2="table_item_even";
				}
				
				
							
		?>	
		 <tr height="20" valign="top" class="<?= $class1 ?>" align="center" onmouseover="this.className='table_item_mouseover'" 
                            onmouseout="this.className='<?= $class1 ?>'">
			<td><?= $no ?></td><Td> <?= $a['user_id'] ?></Td>
            <td><?= getDataById("os_user_role","role_name","id",$a['role_id']) ?></td>
            <td><?= convertStatus($a['status']) ?></td>
			<td align="center">
			 <a href="user_edit.php?id=<?= $a['id'] ?>"><img src="images/icons/edit.gif" border="0"></a>
             <a href="user_delete.php?id=<?= $a['id'] ?>"><img src="images/icons/delete.gif" border="0"></a>
            </td>
		</Tr>
		<? $no++;}} ?>
	</table>
	<script language="javascript">
	function formvalidate(frm)
		{
		
			if(document.frm.user_id.value=="")
			{
				alert("Please enter username");
				document.frm.user_id.focus();
				return false;
			}
			if(document.frm.password.value=="")
			{
				alert("Please enter password");
				document.frm.password.focus();
				return false;
			}
			if(document.frm.password.value.length<6)
			{
				alert("password length must more than 6 characters");
				document.frm.password.focus();
				return false;
			}
			if(document.frm.password.value.indexOf(" ")>-1)
			{
				alert("spaces not allowed!");
				document.frm.password.focus();
				return false;
			}
			if(document.frm.cpassword.value=="")
			{
				alert("Please reenter your password");
				document.frm.cpassword.focus();
				return false;
			}
			if(document.frm.password.value!=document.frm.cpassword.value)
			{
				alert("Password doesn't match, please check");
				document.frm.cpassword.focus();
				return false;
			}
			if(document.frm.branch.value=="")
			{
				alert("Please select branch");
				document.frm.branch.focus();
				return false;
			}
			
			
			return true;
		}
	
	</script>
    <br /><br /><br />
	<form action="<?= $_SERVER['PHP_SELF'] ?>" method="post" name="frm" onSubmit="return formvalidate(this)">
	<table width="100%">
		<tr>
			<td colspan="2"><strong>Add New User</strong></td>
		</tr>
		<tr>
			<td width="12%">Username</td><td width="88%"> : <input type="text" name="user_id" class="text"></td>
		</tr>
		<tr>
			<td>Password</td><td> : <input type="password" name="password" class="text"></td>
		</tr>
		<tr>
			<td>Confirm Password</td><td> : <input type="password" name="cpassword" class="text"></td>
		</tr>
	
         <tr>
            <td>Role</td><td>  : 
            <select name="user_role">
                <option value="">Please Select</option>
                <?
                    $getRole=mysql_query("select * from os_user_role order by role_name asc");
                    
                    if(mysql_num_rows($getRole)>0)
                    {
                        while($r=mysql_fetch_array($getRole,MYSQL_ASSOC))
                        {
                ?>
                <option value="<?= $r['id'] ?>"><?= $r['role_name'] ?></option>
                <? 
                        }
                    } 
                ?>
            </select>
            </td>
        </tr>
        <tr>
            <td>Status</td><td>  : 
            <select name="status">
                <option value="0">Inactive</option>
                <option value="1">Active</option>
            </select>
            </td>
        </tr>
		<tr>
			<td colspan="2">
            <input type="hidden" name="branch_id" value="0">
            <input type="submit" value="Submit" class="button" name="btnsubmit"></td>
		</tr>
	</table>
	</form>
	</td>
</table>

<? require("required/footer.php"); ?>

